package com.sec.android.app.myfiles.facade;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;

import com.sec.android.app.myfiles.facade.cmd.CopyMoveCmd.OperationProgress;
import com.sec.android.app.myfiles.facade.cmd.DecompressRecordsCmd;
import com.sec.android.app.myfiles.facade.cmd.DeleteRecordCmd;
import com.sec.android.app.myfiles.facade.cmd.FinishPickerCmd;
import com.sec.android.app.myfiles.facade.cmd.LockUnlockCmd.LockProgress;
import com.sec.android.app.myfiles.facade.cmd.SpinnerProgressCmd;
import com.sec.android.app.myfiles.facade.cmd.ToSelectModeCmd;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.module.ModuleLoader;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.CategoryType;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationInfo.NavigationMode;
import com.sec.android.app.myfiles.operation.FileOperationFragment.PostOperation;
import com.sec.android.app.myfiles.operation.FileOperator.Operation;
import com.sec.android.app.myfiles.widget.listview.AbsListViewImp;

import org.puremvc.java.multicore.patterns.facade.Facade;

import java.util.ArrayList;
import java.util.List;

public class MyFilesFacade {
    public static void create(int processID) {
        String key = String.valueOf(processID);
        ArrayList<FacadeItem> facadeItemList = ModuleLoader.getInstance().getFacadeItemList();

        Facade facade = Facade.getInstance(key);
        for (FacadeItem item : facadeItemList) {
            facade.registerCommand(item.getName(), item.getCommand());
        }
    }

    public static void destroy(int processID) {
        String key = String.valueOf(processID);
        Facade.removeCore(key);
    }

    public static void executeRecord(int processId, Activity activity, FileRecord record) {
        Object params = new Object[]{processId, activity, record};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.EXECUTE_RECORD, params);
    }

    public static void setShowHiddenFiles(int processId, Context context, AbsMyFilesFragment curFragment) {
        Object params = new Object[]{context, curFragment};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.SHOW_HIDDEN_FILE, params);
    }

    public static void toSelectMode(int processId, AbsMyFilesFragment curFragment, ToSelectModeCmd.SelectModeType selectType) {
        Object params = new Object[]{curFragment, null, -1, selectType};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.SELECT_MODE, params);
    }

    public static void toSelectMode(int processId, AbsMyFilesFragment curFragment, AbsListViewImp listViewImp, int selectedIndex,
                                    ToSelectModeCmd.SelectModeType selectType) {
        Object params = new Object[]{curFragment, listViewImp, selectedIndex, selectType};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.SELECT_MODE, params);
    }

    public static void copymoveRecord(int processId, Context context, Operation operation, OperationProgress progress,
                                      AbsMyFilesFragment fragment, ArrayList<FileRecord> srcRecordList, FileRecord destRecord, PostOperation postOperation) {
        Object params = new Object[]{
                context, operation, progress, fragment, srcRecordList, destRecord, postOperation};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.COPY_MOVE, params);
    }

    public static void copymoveRecord(int processId, Context context, Operation operation, OperationProgress progress,
                                      AbsMyFilesFragment fragment, ArrayList<FileRecord> srcRecordList, FileRecord dstRecord) {
        copymoveRecord(processId, context, operation, progress, fragment, srcRecordList, dstRecord, null);
    }

    public static void moveToPrivate(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList) {
        Object params = new Object[]{processId, context, fragment, recordList};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.MOVE_TO_PRIVATE, params);
    }

    public static void deleteRecord(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList,
                                    DeleteRecordCmd.OperationProgress progress) {
        Object params = new Object[]{context, fragment, recordList, progress};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.DELETE_RECORD, params);
    }

    public static void renameRecord(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList) {
        renameRecord(processId, context, fragment, recordList, null);
    }

    public static void renameRecord(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList, String reservedName) {
        if (recordList != null && !recordList.isEmpty()) {
            Object params = new Object[]{context, fragment, recordList.get(0), reservedName};
            String key = String.valueOf(processId);
            Facade.getInstance(key).sendNotification(FacadeItem.RENAME_RECORD, params);
        }
    }

    public static void createDir(int processId, Context context, AbsMyFilesFragment fragment, FileRecord curDirRecord) {
        Object params = new Object[]{context, fragment, curDirRecord, null};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.CREATE_DIR, params);
    }

    public static void createDir(int processId, Context context, AbsMyFilesFragment fragment, FileRecord curDirRecord, String folderName) {
        Object params = new Object[]{context, fragment, curDirRecord, folderName};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.CREATE_DIR, params);
    }

    public static void compress(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList) {
        Object params = new Object[]{context, fragment, recordList};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.COMPRESS_RECORDS, params);
    }

    public static void decompress(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList) {
        Object params = new Object[]{
                DecompressRecordsCmd.DecompressType.DECOMPRESS, context, processId, fragment, recordList};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.DECOMPRESS_RECORDS, params);
    }

    public static void decompress(int processId, Context context, AbsMyFilesFragment fragment, FileRecord archiveRecord,
                                  ArrayList<FileRecord> decompressList) {
        Object params = new Object[]{
                DecompressRecordsCmd.DecompressType.DECOMPRESS_FROM_PREVIEW, context, processId, fragment, archiveRecord, decompressList};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.DECOMPRESS_RECORDS, params);
    }

    public static void decompressToCurrentFolder(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList) {
        Object params = new Object[]{
                DecompressRecordsCmd.DecompressType.DECOMPRESS_CURRENT_FOLDER, context, processId, fragment, recordList};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.DECOMPRESS_RECORDS, params);
    }

    public static void previewCompress(int processId, Activity activity, AbsMyFilesFragment fragment, FileRecord record) {
        Object params = new Object[]{processId, activity, fragment, record};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.PREVIEW_COMPRESS, params);
    }

    public static void showDetail(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList, boolean fromSearch) {
        Object params = new Object[]{context, fragment, recordList, fromSearch};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.SHOW_DETAIL, params);
    }

    public static void setSortBy(int processId, AbsMyFilesFragment fragment) {
        Object params = new Object[]{fragment, null};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.SET_SORT_BY, params);
    }

    public static void setSortBy(int processId, AbsMyFilesFragment fragment, int sortByType) {
        Object params = new Object[]{fragment, sortByType};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.SET_SORT_BY, params);
    }

    public static void shareVia(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList,
                                boolean isFromConvert) {
        shareVia(processId, context, fragment, recordList, isFromConvert, null, true);
    }

    public static void shareVia(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList,
                                boolean isFromConvert, boolean isShowProgress) {
        shareVia(processId, context, fragment, recordList, isFromConvert, null, isShowProgress);
    }

    public static void shareVia(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList,
                                boolean isFromConvert, String appList, boolean isShowProgress) {
        Object params = new Object[]{
                context, fragment, recordList, isFromConvert, appList, isShowProgress};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.SHARE_VIA, params);
    }

    public static void convert(int processId, AbsMyFilesFragment fragment, FileRecord record) {
        Object params = new Object[]{fragment, record};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.CONVERT, params);
    }

    public static void searchRecords(int processId, String folderName, String fileName) {
        Object params = new Object[]{processId, folderName, fileName};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.SEARCH_RECORDS, params);
    }

    public static void goRoot(int processId, NavigationInfo naviInfo, Context context) {
        Object params = new Object[]{processId, naviInfo, context};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.GO_PRIVATE_ROOT, params);
    }
	
	public static void goHome(int processId, NavigationInfo naviInfo, Context context) {
        Object params = new Object[]{processId, naviInfo, context};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.GO_HOME, params);
    }

    public static void goHomeBlank(int processId, NavigationInfo naviInfo) {
        Object params = new Object[]{processId, naviInfo};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.GO_HOME_BLANK, params);
    }

    public static void launchPicker(int processId, FileRecord homePath, NavigationMode mode, String[] mimeType, String[] extension,
                                    int maxCount, String storagePath, CategoryType category, String prefix) {
        Object params = new Object[]{
                processId, homePath, mode, mimeType, extension, maxCount, storagePath, false, category, prefix};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.LAUNCH_PICKER, params);
    }

    public static void launchPicker(int processId, FileRecord homePath, NavigationMode mode, boolean isDownload) {
        Object params = new Object[]{
                processId, homePath, mode, null, null, 1, null, isDownload, null, null};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.LAUNCH_PICKER, params);
    }

    public static void createDocument(int processId, FileRecord homePath, NavigationMode mode, String mimeType, String name) {
        Object params = new Object[]{processId, homePath, mode, mimeType, name};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.CREATE_DOCUMENT, params);
    }

    public static void clearDownloadHistory(int processId, AbsMyFilesFragment fragment) {
        Object params = new Object[]{fragment};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.CLEAR_DOWNLOAD_HISTORY, params);
    }

    public static void clearRecent(int processId, Context context) {
        Object params = new Object[]{context};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.CLEAR_RECENT, params);
    }

    public static void goSetting(int processId, Context context) {
        Object params = new Object[]{context};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.GO_SETTINGS, params);
    }

    public static void manageShortcut(int processId, int position) {
        Object params = new Object[]{processId, position};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.MANAGE_SHORTCUT, params);
    }

    public static void reload(int processId, Context context, FileRecord record, AbsMyFilesFragment fragment) {
        _reload(processId, context, record, fragment, false);
    }

    public static void _reload(int processId, Context context, FileRecord record, AbsMyFilesFragment fragment, boolean isForceLoad) {
        Object params = new Object[]{context, record, fragment, isForceLoad};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.RELOAD, params);
    }

    public static void addShortcut(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> records) {
        Object params = new Object[]{context, fragment, records};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.ADD_SHORTCUT, params);
    }

    public static void renameHomeItem(int processId, Context context, FileRecord src, FileRecord target) {
        Object params = new Object[]{context, src, target};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.RENAME_HOME_ITEM, params);
    }

    public static void finishPicker(int processId, AbsMyFilesFragment fragment, NavigationInfo naviInfo, FinishPickerCmd.PickerCmd pickerCmd) {
        Object params = new Object[]{processId, fragment, naviInfo, pickerCmd};
        String key = String.valueOf(processId);

        Facade.getInstance(key).sendNotification(FacadeItem.FINISH_PICKER, params);
    }

    public static void lockUnlockRecord(int processId, Context context, AbsMyFilesFragment fragment, FileRecord record,
                                        LockProgress progress) {
        /*
        Activity activity = fragment.getActivity();

        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            if (activity instanceof FileListActivity) {
                ((FileListActivity) activity).setLockObject(new Object[] {
                        progress, context, fragment, bLock, record });
            }

            ActivityCompat.requestPermissions(activity, new String[] {
                Manifest.permission.READ_PHONE_STATE }, FileListActivity.LOCK_PERMISSIONS);
            return;
        }
        */
        Object params = new Object[]{progress, context, fragment, record};
        String key = String.valueOf(processId);

        Facade.getInstance(key).sendNotification(FacadeItem.LOCK_UNLOCK, params);
    }

    public static void execSpinnerProgress(int processId, Context context, SpinnerProgressCmd.OperationProgress progress) {
        Object params = new Object[]{context, progress};
        String key = String.valueOf(processId);

        Facade.getInstance(key).sendNotification(FacadeItem.SPINNER_PROGRESS, params);
    }

    public static void moveKnoxMode(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList, int type) {
        moveKnoxMode(processId, context, fragment, recordList, null, type);
    }

    public static void moveKnoxMode(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList, String destPath, int type) {
        Object params = new Object[]{context, fragment, recordList, destPath, type};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.MOVE_KNOX_MODE, params);
    }

    public static void downloadConfirm(int processId, Context context, AbsMyFilesFragment fragment, FileRecord record) {
        Object params = new Object[]{context, fragment, record};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.DOWNLOAD_CONFIRM, params);
    }

    public static void omaConfirm(int processId, Context context, AbsMyFilesFragment fragment, FileRecord record) {
        Object params = new Object[]{
                context, fragment, record};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.OMA_CONFIRM, params);
    }

    public static void importFromApp(int processId, AbsMyFilesFragment fragment) {
        Object params = new Object[]{
                fragment};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.IMPORT_FROM_APP, params);
    }

    public static void openFolderTree(int processId, Context context, FileRecord record) {
        Object params = new Object[]{context, record};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.FOLDER_TREE_OPEN, params);
    }

    public static void contactUs(int processId, Context context) {
        Object params = new Object[]{context};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.CONTACT_US, params);
    }

    public static void addSuggestion(int processId, Context context, FileRecord record) {
        Object params = new Object[]{
                context, record};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.ADD_SUGGESTION, params);
    }

    public static void deleteSuggestion(int processId, Context context, List<FileRecord> recordList) {
        Object params = new Object[]{context, recordList};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.DELETE_SUGGESTION, params);
    }

    public static void launchOptimizeStorage(int processId, Context context) {
        Object params = new Object[]{context};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.LAUNCH_OPTIMIZE_STORAGE, params);
    }

    public static void goTrash(int processId) {
        Object params = new Object[]{processId};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.GO_TRASH, params);
    }

    public static void showNotEnoughStorage(int processId, Context context, FragmentManager fm, ArrayList<FileRecord> src, String msg, Operation op) {
        Object params = new Object[]{context, fm, src, msg, op};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.SHOW_NOT_ENOUGH_STORAGE, params);
    }

    public static void setShowRecentFiles(int processId, Context context) {
        Object params = new Object[]{context};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.SHOW_RECENT_FILES, params);
    }

    public static void moveToPrivateFolder(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList) {
        Object params = new Object[]{processId, context, fragment, recordList};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.MOVE_TO_PRIVATE_FOLDER, params);
    }

    public static void removeFromPrivateFolder(int processId, Context context, AbsMyFilesFragment fragment, ArrayList<FileRecord> recordList) {
        Object params = new Object[]{processId, context, fragment, recordList};
        String key = String.valueOf(processId);
        Facade.getInstance(key).sendNotification(FacadeItem.REMOVE_FROM_PRIVATE_FOLDER, params);
    }
}
